import './public/toastr'
import './public/index'
import server from './date/url'

let app = new Vue({
  el: '#app',
  data: () => {
    return {
      countDown: 10 * 60,
      ques: [],
      answer: []
    }
  },
  mounted: function() {
    this.getQues()
  },
  methods: {
    timer: function() {
      let that = this

      setInterval(function() {

        if (that.countDown > 0) {
          clearInterval(that.timer)
        }

        that.countDown -= 1

      }, 1000)
    },
    getQues: function() {
      let that = this

      let loading = layer.msg('获取数据中...', {
        icon: 16,
        shade: .6,
        time: 0
      })

      $.ajax({
        url: server,
        type: 'post',
        data: {
          task: 'applicationReport',
          stamp: new Date().getTime()
        }
      })
      .done(function(res) {
        console.log(res)
        
        if (res.status === 'false' || res.status === false) {
          layer.msg(res.message)

          setTimeout(function() {
            window.location = './index.html'
          }, 3000)

          return
        }

        that.ques = res.data.ques
        that.timer()
      })
      .fail(function(res) {

        console.log("error");
      })
      .always(function(res) {
        layer.close(loading)
      })

    },
    inputAnswer: function(index, val) {
      this.answer[index] = val

      console.log(this.answer)
    },
    checkAnswer: function() {
      let i = 0

      if (!this.ques.length) {
        return false
      }

      for (; i < this.ques.length; i++) {
        if (this.answer[i] === undefined) {
          return false
        }
      }

      return true

    },
    submit: function(e) {
      e.preventDefault()

      let that = this

      if (!this.checkAnswer()) {
        layer.msg('请填完问卷')

        return
      }

      let loading = layer.msg('获取数据中...', {
        icon: 16,
        shade: .6,
        time: 0
      })

      $.ajax({
        url: server,
        type: 'post',
        data: {
          task: 'inputQues',
          stamp: new Date().getTime(),
          data: {
            answer: that.answer
          }
        }
      })
      .done(function(res) {
        console.log(res)
        
        if (res.status === 'false' || res.status === false) {
          layer.msg(res.message)

          setTimeout(function() {
            window.location = './index.html'
          }, 3000)

          return
        }

        window.location = './submitSucc.html'
      })
      .fail(function(res) {

        console.log("error");
      })
      .always(function(res) {
        layer.close(loading)
      })

    }
  }
})